﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Micronet.OpenWeb.Com.Controls;
using Micronet.OpenWeb.Common;
using Micronet.OpenWeb.Com.BLL;
using Micronet.OpenWeb.Utils;

namespace Micronet.OpenWeb.OpenWeb.Admin.Views.Shared.OpenPersonality
{
    public partial class HomeProfileLivingProcessEdit : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (OwConstants.QueryString.ID.OwQsIsExist())
            {
                OpenHomeProfileLivingProcessDV.DefaultMode = DetailsViewMode.Edit;
            }
            else
            {
                OpenHomeProfileLivingProcessDV.DefaultMode = DetailsViewMode.Insert;

            }
            OwOpenHomeProfileLivingProcessOds.Inserted += new ObjectDataSourceStatusEventHandler(OwOpenHomeProfileLivingProcessOds_Inserted);
            OwOpenHomeProfileLivingProcessOds.Updated += new ObjectDataSourceStatusEventHandler(OwOpenHomeProfileLivingProcessOds_Updated);
        }

        void OwOpenHomeProfileLivingProcessOds_Updated(object sender, ObjectDataSourceStatusEventArgs e)
        {
            UniqueProcess(sender,e);
        }
        
        void OwOpenHomeProfileLivingProcessOds_Inserted(object sender, ObjectDataSourceStatusEventArgs e)
        {
            UniqueProcess(sender, e);            
        }
        void UniqueProcess(object sender, ObjectDataSourceStatusEventArgs e)
        {
            var IsHomeTowncbb = (OwCheckBox)OpenHomeProfileLivingProcessDV.FindControl("IsHomeTownCbb");
            if (IsHomeTowncbb.Checked.ToString().OwToBoolean() == true)
            {
                var ods = OwOpenHomeProfileLivingProcessBLL.OwGetByDynamic(isHomeTown:true);
                foreach (var entity in ods)
                {
                    var en = OwOpenHomeProfileLivingProcessBLL.OwGetEntity(e.ReturnValue.OwToInt64TryParse());
                    if (en.OpenHomeProfileLivingProcessID != entity.OpenHomeProfileLivingProcessID)
                    {
                        //if (entity.IsHomeTown == true)
                        //{
                            //Response.Write(entity.OpenHomeProfileLivingProcessID.ToString());
                            entity.IsHomeTown = false;
                            OwOpenHomeProfileLivingProcessBLL.OwSave(entity);
                        //}
                    }
                }
            }
        }
    }
}